/**
 * (c): IML.
 *
 */
package org.opentcs.guing.storage;

import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static java.util.Objects.requireNonNull;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import org.opentcs.data.model.visualization.ViewBookmark;

/**
 *
 * @author Philipp Seifert (Fraunhofer IML)
 */
public class ViewBookmarkNameChooser
    extends JPanel {

  /**
   * The list model.
   */
  private final DefaultListModel<ViewBookmark> fListModel;
  /**
   * The existing view bookmarks.
   */
  private final List<ViewBookmark> fBookmarks;

  /**
   * Creates a new instance.
   *
   * @param bookmarks The list of bookmarks.
   */
  public ViewBookmarkNameChooser(List<ViewBookmark> bookmarks) {
    fBookmarks = requireNonNull(bookmarks, "bookmark");
    initComponents();

    bookmarksList.setCellRenderer(new ViewBookmarkCellRender());
    fListModel = new DefaultListModel<>();
    bookmarksList.setModel(fListModel);
    updateList();

    handleListSelection(-1);
  }

  private void handleListSelection(int selectedIndex) {
    if (selectedIndex == -1) {
      choosenBookmarkTextField.setText("");
    }
    else {
      choosenBookmarkTextField.setText((bookmarksList.getSelectedValue()).getLabel());
    }
  }

  private void updateList() {
    fListModel.clear();
    Collections.sort(fBookmarks, getComparator());
    for (ViewBookmark bookmark : fBookmarks) {
      fListModel.addElement(bookmark);
    }
  }

  /**
   * Returns the inserted name.
   *
   * @return The name.
   */
  public String getChosenName() {
    return choosenBookmarkTextField.getText();
  }

  private Comparator<ViewBookmark> getComparator() {
    return new Comparator<ViewBookmark>() {

      @Override
      public int compare(ViewBookmark item1, ViewBookmark item2) {
        String s1 = item1.getLabel();
        String s2 = item2.getLabel();
        s1 = s1.toLowerCase();
        s2 = s2.toLowerCase();

        return s1.compareTo(s2);
      }
    };
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    bookmarksLabel = new javax.swing.JLabel();
    bookmarkssScrollPane = new javax.swing.JScrollPane();
    bookmarksList = new javax.swing.JList<ViewBookmark>();
    choosenBookmarkLabel = new javax.swing.JLabel();
    choosenBookmarkTextField = new javax.swing.JTextField();

    setLayout(new java.awt.GridBagLayout());

    bookmarksLabel.setFont(bookmarksLabel.getFont());
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/opentcs/guing/res/labels"); // NOI18N
    bookmarksLabel.setText(bundle.getString("ViewBookmarkNameChooser.label.models")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
    add(bookmarksLabel, gridBagConstraints);

    bookmarkssScrollPane.setPreferredSize(new java.awt.Dimension(258, 130));

    bookmarksList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    bookmarksList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
      public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
        bookmarksListValueChanged(evt);
      }
    });
    bookmarkssScrollPane.setViewportView(bookmarksList);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.weightx = 0.5;
    gridBagConstraints.weighty = 0.5;
    gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
    add(bookmarkssScrollPane, gridBagConstraints);

    choosenBookmarkLabel.setFont(choosenBookmarkLabel.getFont());
    choosenBookmarkLabel.setText(bundle.getString("systemModelNameChooser.label.name")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
    add(choosenBookmarkLabel, gridBagConstraints);

    choosenBookmarkTextField.setAlignmentX(1.0F);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.gridwidth = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.ipadx = 357;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
    add(choosenBookmarkTextField, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

  private void bookmarksListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_bookmarksListValueChanged
    if (evt.getValueIsAdjusting()) {
      return;
    }

    handleListSelection(bookmarksList.getSelectedIndex());
  }//GEN-LAST:event_bookmarksListValueChanged
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel bookmarksLabel;
  private javax.swing.JList<ViewBookmark> bookmarksList;
  private javax.swing.JScrollPane bookmarkssScrollPane;
  private javax.swing.JLabel choosenBookmarkLabel;
  private javax.swing.JTextField choosenBookmarkTextField;
  // End of variables declaration//GEN-END:variables

  private class ViewBookmarkCellRender
      extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(
        JList<?> list, Object value, int index, boolean isSelected,
        boolean cellHasFocus) {

      ViewBookmark bm = (ViewBookmark) value;
      JLabel label = new JLabel(bm.getLabel());

      if (isSelected) {
        label.setOpaque(true);
        label.setBackground(list.getSelectionBackground());
        label.setForeground(list.getSelectionForeground());
      }

      return label;
    }
  }
}
